<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var buildTree = function (inorder, postorder) {
        if (!inorder.length) return null
        let root = {
          val: postorder[postorder.length - 1],
          left: null,
          right: null
        }
        let mid = inorder.indexOf(root.val)
        root.left = buildTree(inorder.slice(0, mid), postorder.slice(0, mid))
        root.right = buildTree(inorder.slice(mid + 1), postorder.slice(mid, postorder.length - 1))
        return root
      }
      
    </script>
  </body>
</html>
